package view.vip;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import service.impl.DictionaryServiceImpl;
import service.impl.VipServiceImpl;
import service.impl.WaiterServiceImpl;
import entiey.Vip;
import dao.VipDao;//
import entiey.Waiter;
import service.DictionaryService;
import service.VipService;
import service.WaiterService;
import service.impl.VipServiceImpl;
//import org.lq.sm.service.iml.SupplierServiceImpl;
//import org.lq.sm.view.supplier.V_supplier;
import util.CastUtil;
//import org.lq.sm.view.employee.V_EmpMain;
//import org.lq.sm.view.employee.V_empAdd;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ItemEvent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;

public class V_vipMain extends JFrame {

	private JPanel contentPane;
	
	private JTable table;
	private JTextField textname;
	private JTextField textid;
	private JTable table_1;
	private VipService vs = new VipServiceImpl();
	public int no;
	private Vip vip;

	public Vip getVip() {
		return vip;
	}

	public void setVip(Vip vip) {
		this.vip = vip;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_vipMain frame = new V_vipMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_vipMain() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 883, 634);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		
		table= new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null},
			},
			new String[] {
				"\u5E8F\u53F7", "\u4F1A\u5458\u5361\u53F7", "\u59D3\u540D", "\u751F\u65E5", "\u7535\u8BDD", "\u5730\u5740", "\u7EA7\u522B", "\u6298\u6263"
			}
		));
		scrollPane.setViewportView(table);
		
		JPopupMenu popupMenu = new JPopupMenu();
		//addPopup(table, popupMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("选择");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectedRow = table.getSelectedRow();
				int valueAt = (int) table.getValueAt(selectedRow, 1);
				vip = vs.findById(valueAt);
				System.out.println("--------"+vip);
				dispose();

			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		List<Vip> list = vs.findVipAll();
		initTable(list);;
		

		
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel, BorderLayout.NORTH);
	
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_vipAdd add = new V_vipAdd();
				add.setVisible(true);//把添加窗体显示出来
				add.addWindowListener(new WindowAdapter() {
					public void windowClosed(WindowEvent e) {
						initTable(vs.findVipAll());//关闭窗口,查询全部显示出来列表
					}
					public void windowClosing(WindowEvent e) {
					
					}
				});
				
			}
		});
		btnNewButton.setIcon(new ImageIcon(V_vipMain.class.getResource("/images/add.png")));
		panel.add(btnNewButton);
		
		
		
	//---------------------修改功能--------	---------------------
		
		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				int index =  table.getSelectedRow();
				if(index >= 0) {
//					Object v = table.getValueAt(index, 1);
//					int id = CastUtil.castInt(v);
					no = CastUtil.castInt(table.getValueAt(index, 1));
					V_vipAdd vipupdate = new V_vipAdd();
					vipupdate.setNo(no);
					vipupdate.setBounds(V_vipMain.this.getX()+100, V_vipMain.this.getY(), vipupdate.getWidth(), vipupdate.getHeight());
					vipupdate.setVisible(true);
					vipupdate.addWindowListener(new WindowAdapter() {
						public void windowClosed(WindowEvent e) {
							initTable(vs.findVipAll());
					
						};
					});
				}else {
					JOptionPane.showMessageDialog(V_vipMain.this, "请选择会员后,单击修改!");
				}
			}
		});
		
		
		btnNewButton_1.setIcon(new ImageIcon(V_vipMain.class.getResource("/images/modify.png")));
		panel.add(btnNewButton_1);
	
	//---------------------------修改功能结束----------------------------------
		
		
	//------------------------------删除功能开始-----------------------------------------
		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if(index>=0) {
					int n =JOptionPane.showConfirmDialog(V_vipMain.this, "确定删除"+table.getValueAt(index, 1)+"会员吗?","删除",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
					if(n==0) {
						int id = CastUtil.castInt(table.getValueAt(index, 0));
						if(new VipServiceImpl().deleteVip(id)) {
							JOptionPane.showMessageDialog(V_vipMain.this, "删除会员成功!");
							initTable(vs.findVipAll());
						}else {
							JOptionPane.showMessageDialog(V_vipMain.this, "删除会员失败!");
						}
					}
				}else {
					JOptionPane.showMessageDialog(V_vipMain.this, "请选择会员!");
				}

			}
		});
		
		btnNewButton_2.setIcon(new ImageIcon(V_vipMain.class.getResource("/images/delete.png")));
		panel.add(btnNewButton_2);
	//---------------------------------删除功能结束------------------------------------	
		JButton btnNewButton_3 = new JButton("级别设置");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_level level=new V_level();
				level.setBounds(V_vipMain.this.getX(), V_vipMain.this.getY(), level.getWidth(), level.getHeight());
				level.setVisible(true);
			} 
		});
		panel.add(btnNewButton_3);
		
		JLabel lblNewLabel = new JLabel("姓名:");
		panel.add(lblNewLabel);
		
		textname = new JTextField();
		panel.add(textname);
		textname.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("卡号:");
		panel.add(lblNewLabel_1);
		
		textid = new JTextField();
		panel.add(textid);
		textid.setColumns(10);
		
		JButton btnNewButton_4 = new JButton("查询");
		btnNewButton_4.addActionListener(new ActionListener() {
	
		public void actionPerformed(ActionEvent e) {
			
			String value = textname.getText();
			String textId = textid.getText();
			VipService ss = new VipServiceImpl();
			if (!(value.equals(""))&& textId.equals("")) {
				List<Vip> query = ss.query(value);
				initTable(query);//如果姓名框内不为空,卡号框内为空,则调用通过姓名模糊查询 
				
			}else if (value.equals("")&& !(textId.equals(""))) {
				Vip findBycard = ss.findBycard(CastUtil.castInt(textId));				
				initTable(findBycard);//如果姓名框内为空,卡号框内不为空,则调用通过卡号查询
				
			}else if (value.equals("")&& textId.equals("")) {
				List<Vip> findVipAll = ss.findVipAll();
				initTable(findVipAll);//如果姓名框,卡号框都为空,则调用查询全部显示出来列表
			}
		}
	});
		btnNewButton_4.setBounds(526, 22, 93, 23);
		panel.add(btnNewButton_4);
		btnNewButton_4.setIcon(new ImageIcon(V_vipMain.class.getResource("/images/search.png")));
		
		
	}
	
//------------------------------------------------------------------------
	
	
	public void initTable(Vip s) {
		DictionaryService ds = new DictionaryServiceImpl();
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for(int i = 0;i< count;i++) {
			dtm.removeRow(0);
		}
		dtm.addRow(new Object[] {
					s.getNo(),
					s.getCard(),
					s.getName(),
					s.getBirthday(),
					s.getTel(),
					s.getAddress(),
					ds.getDictionaryById(s.getLevel()).getName(),
					s.getDiscount()
		});
		}		
		
//	
//	public void initTable() {
//		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
//		int count = dtm.getRowCount();
//		for(int i = 0;i< count;i++) {
//			dtm.removeRow(0);
//		}
//
//		VipService service = new VipServiceImpl();
//		
//		List<Vip> list = service.findVipAll();
//		for (Vip s : list) {
//			dtm.addRow(new Object[] {
//					s.getNo(),
//					s.getCard(),
//					s.getName(),
//					//s.getGender(),
//					s.getBirthday(),
//					s.getTel(),
//					s.getAddress(),
//					s.getLevel(),
//					s.getDiscount()
//					
//			});
//		}
//		
//	}

	
	public void initTable(List<Vip> list) {
		DictionaryService ds = new DictionaryServiceImpl();
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for(int i = 0;i< count;i++) {
			dtm.removeRow(0);
		}

		for (Vip s : list) {
			dtm.addRow(new Object[] {
					s.getNo(),
					s.getCard(),
					s.getName(),
					//s.getGender(),
					s.getBirthday(),
					s.getTel(),
					s.getAddress(),
					//s.getLevel(),
					ds.getDictionaryById(s.getLevel()).getName(),
					
					s.getDiscount()
			});
		}
	}
	

//	
//	private static void addPopup(Component component, final JPopupMenu popup) {
//		component.addMouseListener(new MouseAdapter() {
//			public void mousePressed(MouseEvent e) {
//				if (e.isPopupTrigger()) {
//					showMenu(e);
//				}
//			}
//			public void mouseReleased(MouseEvent e) {
//				if (e.isPopupTrigger()) {
//					showMenu(e);
//				}
//			}
//			private void showMenu(MouseEvent e) {
//				popup.show(e.getComponent(), e.getX(), e.getY());
//			}
//		});
//	}
}

//---------------------------------------------------------------------------------------------
